import { Component, Vue, Inject } from 'vue-property-decorator';

import { ICityList } from '@/shared/model/city-list.model';
import CityListService from './city-list.service';
import AlertService from '@/shared/alert/alert.service';

@Component
export default class CityListDetails extends Vue {
  @Inject('cityListService') private cityListService: () => CityListService;
  @Inject('alertService') private alertService: () => AlertService;

  public cityList: ICityList = {};

  beforeRouteEnter(to, from, next) {
    next(vm => {
      if (to.params.cityListId) {
        vm.retrieveCityList(to.params.cityListId);
      }
    });
  }

  public retrieveCityList(cityListId) {
    this.cityListService()
      .find(cityListId)
      .then(res => {
        this.cityList = res;
      })
      .catch(error => {
        this.alertService().showHttpError(this, error.response);
      });
  }

  public previousState() {
    this.$router.go(-1);
  }
}
